package com.example.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.example.web.tool.FileUtils;

@RestController
public class FilesController {
	Map<String,Object> map = new  HashMap<String, Object>();
	
	@Autowired
	FileUtils FileUtils;
	
	@PostMapping(value="/upload",produces=MediaType.APPLICATION_JSON_VALUE)
	public Map<String,Object> upload(MultipartFile file,HttpServletRequest request) {
		
		String path = request.getServletContext().getRealPath("/");
		System.out.println("path==" + path);
		String path2 = FileUtils.oneUpload(file, path);
		System.out.println("返回"+path2);
		map.put("name",path2);
		return map;
	}
	
	@PostMapping(value = "/batch",produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String,Object> upload(HttpServletRequest request) {
		List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
		List<String>  path2=FileUtils.manyUpload(files);
		System.out.println("返回"+path2);
		map.put("name",path2);
		return map;
	}
	
	@RequestMapping("/aaa")
	public Map<String,Object> aaa() {
		
		map.put("name", "asdasd");
		return map;
	}
}
